import sys
import detect_utils

def Distance(point_a,point_b):
    return detect_utils.getrdistance(point_a,point_b)
def dtw(traj_a,traj_b):
    La=len(traj_a)
    Lb=len(traj_b)
    M=[[Distance(traj_a[i],traj_b[j]) for i in range(La)]for j in range(Lb)]
    D=[[0 for i in range(La+1)]for j in range(Lb+1)]
    D[0][0]=0
    for i in range(1,La+1):
        D[0][i]=sys.maxsize
    for j in range(1,Lb+1):
        D[j][0]=sys.maxsize
    for i in range(1,Lb+1):
        for j in range(1,La+1):
            D[i][j]=M[i-1][j-1]+min(D[i-1][j],D[i-1][j-1],D[i][j-1])
    minD=D[Lb][La]
    return minD
